package com.oedtech.esl.utils;

import com.oedtech.esl.model.esl.EslFirmware;
import com.oedtech.esl.model.esl.EslTab;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Arrays;
import java.util.Base64;

/**
 * @Author QiuLongYu
 * @Date 2019/8/6
 * 图片处理工具
 */

public class ImageUtils {
    final static Base64.Decoder decoder = Base64.getDecoder();
    private final static Logger LOG = LoggerFactory.getLogger(ImageUtils.class);



    public static void base64ToBmp(String imgPath){
        File file = new File(imgPath);
        if(!file.exists()){
            return;
        }
       byte[]pngImg= base64Decode(imgPath);
    }



    public static byte[] base64Decode(String imgPath){
        FileInputStream fis=null;
        try {
            File file = new File(imgPath);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdir();
            }
            fis=new FileInputStream(file);
            byte[]b=new byte[(int)file.length()];
            fis.read(b);

            byte[]img=decoder.decode(b);
            return img;
        }  catch (IOException e) {
            LOG.error("",e);
            return null;
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                   LOG.error("",e);
                }
            }
        }
    }

    /**
     * 图片转为协议要求的点阵数据
     * @param eslFirmware 价签固件ID
     * @param binPath     点阵数据存放路径
     * @throws IOException
     */
    public static void imgTobin(EslFirmware eslFirmware,String binPath,byte[]img) throws IOException {
        if(eslFirmware==null)return;
        int index=0;
        byte[] imgBin = new byte[100*1024];
        index+=34;
        imgBin[index++]=(byte)0xf0;
        String color=eslFirmware.getScreen_color();
        byte pageNum= 1;
        if(color.contains("R")||color.contains("Y")){
            pageNum= 2;
        }
        int x = eslFirmware.getResolution_x();
        int y = eslFirmware.getResolution_y();
        imgBin[index++]=(byte)x;
        imgBin[index++]=(byte)(x>>8);
        imgBin[index++]=(byte)y;
        imgBin[index++]=(byte)(y>>8);
        imgBin[index++]=(byte)0xff;//第五字节保留
        imgBin[index++]=pageNum;
        for(byte i=0;i<pageNum;i++){
            imgBin[index++]=i;
            byte[]b=imgToByte(img,pageNum-1,i);
            System.arraycopy(b,0,imgBin,index,b.length);
            index+=b.length;
        }
        byte[]result=Arrays.copyOf(imgBin,index);
        File file=new File(binPath);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdir();
        }
            OutputStream os=new FileOutputStream(file);
            os.write(result);
            os.close();

    }
    public static byte[] imgToByte(byte[]img,int screenType,int color){

        try {
            return jpg2bin(null,screenType,color,img);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }










    public static byte[] testGetByte(int color){



        String path="sn_img_bin/BW.bin";
        if(color==1){
            path="sn_img_bin/RW.bin";
        }
        File file=new File(path);
        byte[] img=new byte[(int)file.length()];
        try {
            InputStream is=new FileInputStream(file);
            is.read(img);
            for(int i=0;i<img.length;i++){
                img[i]=(byte)(~img[i]);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return img;


    }


    public static byte[] jpg2bin(String image, int screenType,int color,byte[]img) throws Exception {
        int[] rgb = new int[3];
//        File file = new File(image);
//        FileOutputStream bfile = new FileOutputStream(image + ".bin");
        BufferedImage bi = null;
        ByteArrayInputStream in = new ByteArrayInputStream(img);
        try {
            bi = ImageIO.read(in);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        int width = bi.getWidth();
        int height = bi.getHeight();
        int addHeight=height%8==0?height:height+height%8;  //一个字节存储8位点阵，图像y轴不能整除8则需要在后面补零以达到被8整除
        byte[] bbuf = new byte[width * addHeight / 8];
        byte b = 0;
        int c = 0;

        for (int i = 0; i < width; i++) {
            for (int j = 0; j <  addHeight; j++) {
                b <<= 1;
                if(j>=height){
                    b|=0;
                }else {

                    int pixel = bi.getRGB(i, j);
                    rgb[0] = (pixel & 0xff0000) >> 16;
                    rgb[1] = (pixel & 0xff00) >> 8;
                    rgb[2] = (pixel & 0xff);


                    if(screenType==0){
                        if(rgb[0]>127&&rgb[1]>127&&rgb[2]>127){

                        }else{
                            b|=1;
                        }
                    }else {
                        if(color==0){
                            if(rgb[0]<127&&rgb[1]<127&&rgb[2]<127) b |= 1;

                        }else {
                            if(rgb[0]<127&&rgb[1]<127&&rgb[2]<127||rgb[0]>127&&rgb[1]>127&&rgb[2]>127){

                            }else{
                                b|=1;
                            }
//                        if(rgb[0]>=0xcc&&v<200) b |= 1;

                        }
                    }
                }
                c++;
                if(c % 8 == 0)
                {
                    bbuf[c / 8 - 1] = b;
                    b = 0;

                }
            }
        }
//        Image tempImg=Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width,height, bbuf, 0, height));
//        BufferedImage images = new BufferedImage(tempImg.getWidth(null),tempImg.getHeight(null), BufferedImage.TYPE_USHORT_GRAY );
//        images.createGraphics().drawImage(tempImg, 0, 0, null);
//        ImageIO.write(images,"PNG",new File(image+"-bin.png"));
//        bfile.write(bbuf);
//        bfile.close();
        return bbuf;
    }


}
